package com.example.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.*;
import com.example.demo.service.*;
import com.example.demo.vo.DataView;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/sending")
public class AllInfoSendingController {

    @Value("${qrCode.voices}")
    private String voicePath;

    @Value("${qrCode.videos}")
    private String videoPath;

    @Value("${qrCode.images}")
    private String imagesPath;

    @Autowired
    private ContentCcService contentCcService;

    @Autowired
    private PeopleCcService peopleCcService;

    @Autowired
    private VoiceCcService voiceCcService;

    @Autowired
    private PictureCcService pictureCcService;

    @Autowired
    private VideoCcService videoCcService;


    @RequestMapping(value = "/sendingContent")
    public DataView sendingContent(HttpServletRequest request)
    {
        DataView dataView = new DataView();
        String qrCode = request.getHeader("qrCode");
        if(null == qrCode)
        {
            dataView.setCode(400);
            return dataView;
        }
        QueryWrapper<ContentCc> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(true,"qr_code_id",qrCode);
        ContentCc one = contentCcService.getOne(queryWrapper);
        if(null == one)
        {
            dataView.setCode(300);
            dataView.setMsg("没有内容");
            return dataView;
        }
        String content = one.getContent();
        dataView.setData(content);
        dataView.setCode(200);
        dataView.setMsg("内容获取成功");
        return dataView;
    }

    @RequestMapping(value = "/sendingPeopleName")
    public DataView sendingPeopleName(HttpServletRequest request)
    {
        DataView dataView = new DataView();
        String qrCode = request.getHeader("qrCode");
        if(null == qrCode)
        {
            dataView.setCode(400);
            return dataView;
        }
        QueryWrapper<PeopleNameCc> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(true,"qr_code_id",qrCode);
        PeopleNameCc one = peopleCcService.getOne(queryWrapper);
        if(null == one)
        {
            dataView.setCode(300);
            dataView.setMsg("没有内容");
            return dataView;
        }
        String peopleName = one.getPeopleName();
        dataView.setData(peopleName);
        dataView.setCode(200);
        dataView.setMsg("内容获取成功");
        return dataView;
    }


    @RequestMapping(value = "/sendingVoice")
    public DataView sendingVoice(HttpServletRequest request)
    {
        DataView dataView = new DataView();
        String qrCode = request.getHeader("qrCode");
        if(qrCode == null)
        {
            dataView.setCode(400);
            dataView.setMsg("请扫描二维码");
            return dataView;
        }
        QueryWrapper<VoiceCc> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(true,"qr_code_id",qrCode);
        VoiceCc one = voiceCcService.getOne(queryWrapper);
        if(null == one)
        {
            dataView.setCode(300);
            dataView.setMsg("录音内容为空");
            return dataView;
        }
        String voicePath1 = one.getVoicePath();
        dataView.setData(voicePath1);
        dataView.setCode(200);
        dataView.setMsg("录音路径获取成功");
        return dataView;
    }


    @RequestMapping(value = "/sendingPicture")
    public DataView sendingPicture(HttpServletRequest request)
    {
        DataView dataView = new DataView();
        String qrCode = request.getHeader("qrCode");
        if(null == qrCode)
        {
            dataView.setCode(400);
            dataView.setMsg("请扫描二维码");
            return dataView;
        }
        QueryWrapper<PictureCc> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(true,"qr_code_",qrCode);
        List<PictureCc> list = pictureCcService.list(queryWrapper);
        if(list.isEmpty())
        {
            dataView.setCode(300);
            dataView.setMsg("图片路径内容为空");
            return dataView;
        }
        List<String> listsPicturePath = new ArrayList<>();
        for (PictureCc pictureCc : list)
        {
            listsPicturePath.add(pictureCc.getPicturePath());
        }
        dataView.setMsg("图片路径获取成功");
        dataView.setCode(200);
        dataView.setData(listsPicturePath);
        return dataView;
    }

    public DataView sendingVideo(HttpServletRequest request)
    {
        DataView dataView = new DataView();
        String qrCode = request.getHeader("qrCode");
        if(null == qrCode)
        {
            dataView.setCode(400);
            dataView.setMsg("请扫描二维码");
            return dataView;
        }
        QueryWrapper<VideoCc> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(true,"qr_code_id",qrCode);
        List<VideoCc> list = videoCcService.list(queryWrapper);
        if(list.isEmpty())
        {
            dataView.setCode(300);
            dataView.setMsg("视频内容为空");
            return dataView;
        }
        List<String> listVideoPath = new ArrayList<>();
        for (VideoCc videoCc : list)
        {
            listVideoPath.add(videoPath);
        }
        dataView.setCode(200);
        dataView.setMsg("视频路径获取成功");
        dataView.setData(listVideoPath);
        return dataView;
    }

}
